<?php
/**
 * Created by PhpStorm.
 * User: tangmusen
 * Date: 2017/10/30
 * Time: 11:08
 */
namespace app\api\controller;


use app\admin\model\Members;
use app\home\controller\Biapi;
use think\Config;
use think\Controller;
use think\Db;
use think\Request;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
class Personal extends Base
{
    public function _initialize()
    {
//        $uid = \request()->post('uid');
//        $token = \request()->post('token');
//        check_token($uid, $token);
    }


    /**
     * 获取个人信息
     */
    public function get_information(Request $request){
        $uid          = $request->post('uid');
        $field        = 'id,token,head,hot,city,nickname,sex,mobile,province,sign,district,money,create_at,is_room,is_say';
        $data         = Db::name('member')->field($field)->where(array('id'=>$uid))->find();
        json_return(200, '成功', $data);
    }

    /**
     * 修改个人信息
     */
    public function edit_personal(Request $request){
        $uid            = $request->post('uid');
        $sign           = $request->post('sign');
        $nickname       = $request->post('nickname');
        $sex            = $request->post('sex');
        $city           = $request->post('city');
        $province       = $request->post('province');
        $district       = $request->post('district');

        if(!$uid){
            json_return(204,'缺少参数');
        }
        if($nickname){
            $data['nickname'] = $nickname;
        }
        if($sign){
            $data['sign'] = $sign;
        }
        if($sex){
            $data['sex'] = $sex;
        }
        if($city){
            $data['city'] = $city;
        }
        if($province){
            $data['province'] = $province;
        }
        if($district){
            $data['district'] = $district;
        }
        $data['update_at'] = time();
        $result = Db::name('member')->where(array('id'=>$uid))->update($data);
        $return_data = $data;
        if($result){
            json_return(200,'修改成功',$return_data);
        }else{
            json_return(500,'修改失败');
        }
    }

    /**
     * 用户提现
     */
    public function do_withdrawals(Request $request){
        $uid           = $request->post('uid');
        $money         = $request->post('money');
        if(!$uid ||!$money ){
            json_return(204,'缺少参数');
        }
        $user = Members::get($uid);
        $my_money = $user['money'];
        $m_status = $user['m_status'];
        if($m_status==1){
            json_return(201,'您的账号已被冻结!');
        }
        if($my_money<$money){
            json_return(207,'余额不足');
        }
        if(empty($user['bank'])){
            json_return(201,'请先绑定银行卡');
        }
        Db::startTrans();
        try {
          
            $result0 = Db::name('member')
                ->where(array('id'=>$uid))
                ->setDec('money',$money);
            $data['uid']          = $uid;
            $data['username']     = $user['true_name'];
            $data['bank_name']    = $user['bank'];
            $data['account_bank'] = $user['bank_card'];
            $data['branch_name']  = '银行卡';
            $data['money']        = $money;
            $data['way']          = 3;
            $data['create_at']    = time();
            $result = Db::name('withdrawals')->insert($data);
            addDetail($uid,$money,2);
            Db::commit();
            json_return(200,'申请提现成功');
        }catch (\Exception $e){
            Db::rollback();
            json_return(500,'申请提现失败');
        }
    }

    /**
     * 充值记录
     */
    public function recharge_record(Request $request){
        $uid     = $request ->post('uid');

        $page    = $request ->post('page')?$request->post('page'):1;

        if(!$uid){
            json_return(204,'缺少参数');
        }

        $limit = 20;
        $start = ($page-1)*$limit;
        $data = Db::name('recharge')
            ->field('id,create_at,money,status,way')
            ->where(array('uid'=>$uid))
            ->limit($start,$limit)
            ->order('id desc')
            ->select();
        foreach ($data as $k=>$v){
            if($v['status']==1) {
                $data[$k]['status'] = "申请中";
            }
            if($v['status']==2) {
                $data[$k]['status'] = "成功";
            }
            if($v['status']==3) {
                $data[$k]['status'] = "失败";
            }
        }
        $count = Db::name('recharge')
            ->where(array('uid'=>$uid))
            ->count();

        $return_data['data']       = $data;
        $return_data['now_page']   = $page;
        $return_data['per_page']   = $limit;
        $return_data['total_page'] = ceil($count/$limit);
        if($data) {
            json_return(200, '成功', $return_data);
        }else{
            json_return(205, '暂无数据');
        }

    }

    /**
     * 获取提现记录
     */
    public function withdrawals_record(Request $request){

        $uid     = $request->post('uid');
        $page    = $request ->post('page')?$request->post('page'):1;
        $limit   = 20;
        $start   = ($page-1)*$limit;
        $data = Db::name('withdrawals')
            ->field('id,money,bank_name,status,create_at,way')
            ->where(array('uid'=>$uid))
            ->limit($start,$limit)
            ->order('id desc')
            ->select();
        $count = Db::name('withdrawals')
            ->where(array('uid'=>$uid))
            ->count();
        if(!$data){
            json_return(205,'暂无数据');
        }
        foreach ($data as $k=>$v){
         $data[$k]['create_at'] = date('Y-m-d H:i:s',$v['create_at']);
        }
        $return_data['back_data']       = $data;
        $return_data['now_page']   = $page;
        $return_data['per_page']   = $limit;
        $return_data['total_page'] = ceil($count/$limit);
        json_return(200,'成功',$return_data);

    }

    /**
     * 获取银行列表
     */
    public function get_banks(){
        $data = Db::name('banks')->select();
        json_return(200,'成功',$data);
    }

    /**
     * 修改登陆密码
     */
    public function edit_pass(Request $request){

        $uid        = $request->post('uid');
        $old_pass   = $request->post('old_pass');
        $new_pass1  = $request->post('new_pass1');
        $new_pass2  = $request->post('new_pass2');
        if(!$old_pass || !$new_pass1 || !$new_pass2 || !$uid ){
            json_return(204,'缺少参数');
        }
        if($new_pass1 != $new_pass2){
            json_return(201,'两次密码不同');
        }
        $my_pass = Db::name('member')->where(array('id'=>$uid))->value('password');

        if(md5($old_pass) != $my_pass){
            json_return(201,'原密码不正确');
        }
        $result = Db::name('member')
            ->where(array('id'=>$uid))
            ->update(array('password'=>md5($new_pass1)));
        if($result){
            json_return(200,'修改密码成功');
        }else{
            json_return(500,'失败');
        }


    }

    /**
     * 获取我的消息
     */
    public function get_my_notice(Request $request){
        $uid   = $request->post('uid');
        $page  = $request->post('page')?$request->post('page'):1;
        if(!$uid){
            json_return(204,'缺少参数');
        }
        $limit = 5;
        $start = ($page-1)*$limit;
        $data  = Db::name('member_msg')
            ->alias('s')
            ->where(array('uid'=>$uid))
            ->order('id desc')
            ->limit($start,$limit)
            ->select();
        $total = Db::name('member_msg')->where(array('uid'=>$uid))->count();
        $return_data['data']       = $data;
        $return_data['now_page']   = $page;
        $return_data['per_page']   = $limit;
        $return_data['total_page'] = ceil($total/$limit);
        if($data) {
            json_return(200, '成功', $return_data);
        }else{
            json_return(201, '暂无数据');
        }
    }

    /**
     * 修改头像
     */
    public function upload_head(Request $request){
       $uid    = $request->post('uid');
       if(!$uid){
           json_return(201, '缺少参数');
       }
       $result =  $this->qi_niu_upload();
        if($result['status']==1){
            $back['head'] = $result['msg'];
            Db::name('member')->where('id',$uid)->update($back);
            json_return(200, '修改头像成功',$back);
        }else{
            json_return(200, '修改头像失败');
        }
    }

    /**
     * 我的关注
     */
    public function get_my_attention(Request $request){
        $uid           = $request->post('uid');
        $attention_arr =  Db::name('attention')->where('boss_uid',$uid)->column('fans_uid');
        if(!$attention_arr){
            json_return(201,'您还未关注任何人哦');
        }
        $w = [];
        $w['id'] = array('in',$attention_arr);
        $page    = $request ->post('page')?$request->post('page'):1;
        $limit   = 20;
        $start   = ($page-1)*$limit;
        $data = Db::name('member')
            ->field('id,nickname,head')
            ->where($w)
            ->limit($start,$limit)
            ->order('id desc')
            ->select();
        if(!$data){
            json_return(205,'暂无数据');
        }
        foreach ($data as $k=>$v){
            $fans_arr = Db::name('attention')->where('boss_uid',$v['id'])->column('fans_uid');
            if($fans_arr && in_array($uid,$fans_arr)){
                $data[$k]['hz_notice'] = 1;
            }else{
                $data[$k]['hz_notice'] = 0;
            }

        }
        $count = Db::name('member')
            ->where($w)
            ->count();
        $return_data['back_data']  = $data;
        $return_data['now_page']   = $page;
        $return_data['per_page']   = $limit;
        $return_data['total_page'] = ceil($count/$limit);
        json_return(200,'成功',$return_data);
    }

    /**
     * 我的粉丝
     */
    public function get_my_fans(Request $request){
        $uid           = $request->post('uid');
        //粉丝的id
        $attention_arr =  Db::name('attention')->where('fans_uid',$uid)->column('boss_uid');
        if(!$attention_arr){
            json_return(201,'您还未成为任何人的粉丝哦');
        }
        $w = [];
        $w['id'] = array('in',$attention_arr);
        $page    = $request ->post('page')?$request->post('page'):1;
        $limit   = 20;
        $start   = ($page-1)*$limit;
        $data = Db::name('member')
            ->field('id,nickname,head')
            ->where($w)
            ->limit($start,$limit)
            ->order('id desc')
            ->select();
        if(!$data){
            json_return(205,'暂无数据');
        }
        $fans_arr = Db::name('attention')->where('boss_uid',$uid)->column('fans_uid');
        foreach ($data as $k=>$v){
            if($fans_arr && in_array($v['id'],$fans_arr)){
                $data[$k]['hz_notice'] = 1;
            }else{
                $data[$k]['hz_notice'] = 0;
            }

        }
        $count = Db::name('member')
            ->where($w)
            ->count();
        $return_data['back_data']  = $data;
        $return_data['now_page']   = $page;
        $return_data['per_page']   = $limit;
        $return_data['total_page'] = ceil($count/$limit);
        json_return(200,'成功',$return_data);
    }

    /**
     * 昵称搜索
     */
    public function get_user_nickname(Request $request){
        $uid      = $request->post('uid');
        $nickname = $request->post('nickname');
        $w['nickname'] = $nickname;
        $page    = $request ->post('page')?$request->post('page'):1;
        $limit   = 20;
        $start   = ($page-1)*$limit;
        $data = Db::name('member')
            ->field('id,nickname,head')
            ->where($w)
            ->limit($start,$limit)
            ->order('id desc')
            ->select();
        if(!$data){
            json_return(205,'暂无数据');
        }

        $uids = Db::name('attention')->where('boss_uid',$uid)->column('fans_uid');
        foreach ($data as $k=>$v){
            $fans_arr = Db::name('attention')->where('boss_uid',$v['id'])->column('fans_uid');
            if($fans_arr && in_array($uid ,$fans_arr)){
                $data[$k]['hz_notice'] = 1;
            }else{
                $data[$k]['hz_notice'] = 0;
            }
            if(in_array($v['id'],$uids)){
                $data[$k]['is_attention'] = 0;
            }else{
                $data[$k]['is_attention'] = 1;
            }

        }
        $count = Db::name('member')
            ->where($w)
            ->count();
        $return_data['back_data']  = $data;
        $return_data['now_page']   = $page;
        $return_data['per_page']   = $limit;
        $return_data['total_page'] = ceil($count/$limit);
        json_return(200,'成功',$return_data);
    }

    /**
     * 上传一张图片
     */
    public function upload_one(){
        $file = request()->file('file');
        if(!$file){

            json_return(204, '请选择上传图片');
        }
        $path = ROOT_PATH . 'public' . DS . 'uploads'. DS .'cate_img';
        $info = $file->move($path);
        if($info){
            $picd = 'cate_img/'.$info->getSaveName();
            $pic  = Config::get('img_url').$picd;
            $data = ["pic" => $pic,'picd'=>$picd];
            json_return(200, '上传成功',$data);
        }else{
            json_return(201, '上传失败');
        }
    }

    /**
     * 退出登陆
     */
    public function logout(Request $request){
        $uid           = $request->post('uid');
        if(!$uid){
            json_return(204, '缺少参数');
        }
        $data['token'] = 0;
        Db::name('member')->where(array('id'=>$uid))->update($data);
        json_return(200,'成功');
    }

    /**
     * 关注某人
     */
    public function attention_someone(Request $request){
         $uid      = $request->post('uid');
         $fans_uid = $request->post('fans_uid');
         if(!$fans_uid){
            json_return(204,'缺少参数');
          }
         $insert = [
           'boss_uid'=>$uid,
           'fans_uid'=>$fans_uid
         ];
         $is_attention = Db::name('attention')->where($insert)->find();
         if($is_attention){
             json_return(201,'已经关注过了');
         }
         $result = Db::name('attention')->insert($insert);
         if($result){
             json_return(200,'关注成功');
         }else{
             json_return(201,'关注失败');
         }
    }
    
    /**
     * 取消关注某人
     */
    public function attention_cancel(Request $request){
        $uid      = $request->post('uid');
        $fans_uid = $request->post('fans_uid');
        if(!$fans_uid){
            json_return(204,'缺少参数');
        }
        $insert = [
            'boss_uid'=>$uid,
            'fans_uid'=>$fans_uid
        ];
        $result = Db::name('attention')->where($insert)->delete();
        if($result){
            json_return(200,'取消关注成功');
        }else{
            json_return(201,'取消关注失败');
        }
    }

    /**
     * 根据id获取某人的信息
     */
    public function get_information_by_id(Request $request){
        $uid = $request->post('uid');
        $search_uid = $request->post('search_uid');
        if(!$search_uid){
            json_return(204,'缺少参数');
        }
        $user_info  = Db::name('member')->field('id,head,nickname')->where('id',$search_uid)->find();
        $uids = Db::name('attention')->where('boss_uid',$uid)->column('fans_uid');
        if(in_array($search_uid,$uids)){
            $user_info['is_attention'] = 0;
        }else{
            $user_info['is_attention'] = 1;
        }

        if($user_info){
            json_return(200,'获取信息成功',$user_info);
        }else{
            json_return(201,'暂无信息');
        }
    }

    /**
     * 绑定银行卡
     */
    public function bang_card(Request $request){
        $uid       = $request->post('uid');
        $true_name = $request->post('true_name');
        $bank      = $request->post('bank');
        $bank_card = $request->post('bank_card');
        if(!$uid || !$true_name || !$bank ||!$bank_card){
            json_return(204,'缺少参数');
        }
        if(!is_card($bank_card)){
            json_return(201,'银行卡号格式不正确');
        }
        $insert = [
            'true_name'=>$true_name,
            'bank'     =>$bank,
            'bank_card'=>$bank_card
        ];
        $result0 = Db::name('member')->where('id',$uid)->update($insert);
        if($result0){
            json_return(200,'绑定成功');
        }else{
            json_return(201,'绑定失败');
        }
    }

    /**
     * 获取绑定的银行卡信息
     */
    public function get_bang_info(Request $request){
        $uid = $request->post('uid');
        $bang_info = Db::name('member')
            ->field('true_name,bank,bank_card')
            ->where('id',$uid)
            ->find();
//        $bang_info['true_name'] = '***'. mb_substr($bang_info['true_name'] , -2, 1, 'utf-8');
//        $bang_info['bank_card'] = '***'. mb_substr($bang_info['bank_card'] , -4, 4, 'utf-8');
        json_return(200,'获取绑定信息成功',$bang_info);
    }

    /**
     * 账单明细
     */
    public function billing_details(){
        $uid     = input('post.uid');
        $page    = input('post.page')?input('post.page'):1;
        $limit   = 20;
        $start   = ($page-1)*$limit;
        $maps    = [
            'uid'=>$uid
        ];
        $data = Db::name('detail')
            ->alias('d')
            ->field('d.*,e.title')
            ->join('dl_detail_exp e','e.id=d.exp')
            ->where($maps)
            ->limit($start,$limit)
            ->order('d.id desc')
            ->select();
        $count = Db::name('detail')
            ->where($maps)
            ->count();
        if(!$data){
            json_return(205,'暂无数据');
        }

        $return_data['back_data']  = $data;
        $return_data['now_page']   = $page;
        $return_data['per_page']   = $limit;
        $return_data['total_page'] = ceil($count/$limit);
        json_return(200,'成功',$return_data);
    }



}